package com.cyt.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.dao.SmsLogDao;
import com.cyt.sys.model.po.SmsLog;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SmsLogVo;
import com.cyt.sys.service.SmsLogService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 短信平台发送记录-短信平台发送记录Service层实现类
 *
 * @author ding xu
 * @date 2019-12-31 9:12:56
 */
@Service("smsLogService")
public class SmsLogServiceImpl extends BaseServiceImpl<SmsLog> implements SmsLogService {

    @Resource
    private SmsLogDao smsLogDao;

    @Autowired
    public void setBaseDao(SmsLogDao smsLogDao) {
        this.baseDao = smsLogDao;
    }

    /**
     * 查询对象集合
     *
     * @param smsLogVo 对象
     * @return List 对象集合
     * @author ding xu
     * @date 2019-12-31 9:12:56
     */
    @Override
    public List<SmsLog> listExample(SmsLogVo smsLogVo) {
        Example example = new Example(SmsLog.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(smsLogVo.getType())) {
            criteria.andEqualTo("type", smsLogVo.getType());
        }
        if (StringUtils.isNotBlank(smsLogVo.getResult())) {
            criteria.andEqualTo("result", smsLogVo.getResult());
        }
        return smsLogDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param smsLogVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author ding xu
     * @date 2019-12-31 9:12:56
     */
    @Override
    public ReturnResult page(SmsLogVo smsLogVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<SmsLog> list = listExample(smsLogVo);
        PageInfo<SmsLog> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param smsLogVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author ding xu
     * @date 2019-12-31 9:12:56
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(SmsLogVo smsLogVo, SysUser sysUser) {
        if (StringUtils.isBlank(smsLogVo.getId())) {
            SmsLog smsLog = new SmsLog(sysUser);
            smsLog.setApplyId(smsLogVo.getApplyId());
            smsLog.setType(smsLogVo.getType());
            smsLog.setModelId(smsLogVo.getModelId());
            smsLog.setParams(smsLogVo.getParams());
            smsLog.setUserId(smsLogVo.getUserId());
            smsLog.setResult(smsLogVo.getResult());
            smsLog.setModule(smsLogVo.getModule());
            smsLog.setValidState(smsLogVo.getValidState());
            smsLogDao.insert(smsLog);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            SmsLog smsLog = smsLogDao.selectByPrimaryKey(smsLogVo.getId());
            smsLog.setApplyId(smsLogVo.getApplyId());
            smsLog.setType(smsLogVo.getType());
            smsLog.setModelId(smsLogVo.getModelId());
            smsLog.setParams(smsLogVo.getParams());
            smsLog.setUserId(smsLogVo.getUserId());
            smsLog.setResult(smsLogVo.getResult());
            smsLog.setModule(smsLogVo.getModule());
            smsLog.setValidState(smsLogVo.getValidState());
            smsLog.setUpdateTime(new Date());
            smsLog.setUpdateUser(sysUser.getId());
            smsLogDao.updateByPrimaryKey(smsLog);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param smsLogVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author ding xu
     * @date 2019-12-31 9:12:56
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(SmsLogVo smsLogVo, SysUser sysUser) {
        //要修改的字段
        SmsLog smsLog = new SmsLog();
        smsLog.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        smsLog.setUpdateUser(sysUser.getId());
        smsLog.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(SmsLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(smsLogVo.getId())) {
            criteria.andIn("id", Arrays.asList(smsLogVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        smsLogDao.updateByExampleSelective(smsLog, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param smsLogVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author ding xu
     * @date 2019-12-31 9:12:56
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(SmsLogVo smsLogVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(SmsLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(smsLogVo.getId())) {
            criteria.andIn("id", Arrays.asList(smsLogVo.getId().split(",")));
        }
        smsLogDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}